package com.tsingteng.cosfun.ui.costar;


import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterDialogFragment;
import com.tsingteng.cosfun.bean.CommBean;
import com.tsingteng.cosfun.bean.VideoMusicBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.FileDownUtils;
import com.tsingteng.cosfun.utils.MusicUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.dialog.ProgressDialog;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/27
 * qq(872179966)
 */

public class VideoMusicFragment extends BasePresenterDialogFragment<VideoMusicPresenter, VideoMusicContract.IVideoMusicView> implements VideoMusicContract.IVideoMusicView {

    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.rv_rv)
    MyRecycleView rvRv;
    @BindView(R.id.bt_sure)
    TextView btSure;
    private int pageNo = 1;
    private int currentPosition = -1;
    private OnDownVideoMusicListener onDownVideoMusicListener;
    private ProgressDialog progressDialog;
    private String savePath;
    private int musicPosition = -1;





    @Override
    public void showMusicData(CommBean data) {
        if (rvRv != null) rvRv.refreshComplete();
        if (data.getData().size() < 15) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
        }

        mAdapter.addData(data.getData());
        mAdapter.notifyDataSetChanged();
    }


    @Override
    protected void initData() {
        pageNo = 1;
        mPresenter.getMusicVideoData(pageNo, Config.PAGE_SIZE);
        progressDialog = new ProgressDialog(getActivity());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (musicPosition != -1 && mAdapter!=null) {
                    List<VideoMusicBean> mAdapterData = mAdapter.getData();
                    if (mAdapterData.size() > musicPosition) {
                        mAdapterData.get(musicPosition).setSelect(true);
                        rvRv.srcollToPosition(musicPosition);
                        currentPosition = musicPosition;
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        },1000);
    }

    @Override
    protected VideoMusicPresenter createPresenter() {
        return new VideoMusicPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return rvRv;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new VideoMusicAdapter();
    }

    @Override
    protected void refresh() {
        pageNo = 1;
        mPresenter.getMusicVideoData(pageNo, Config.PAGE_SIZE);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        VideoMusicBean item = (VideoMusicBean) adapter.getItem(position);
        item.setSelect(true);
        if (currentPosition != -1 && currentPosition != position) {
            VideoMusicBean oldItem = (VideoMusicBean) adapter.getItem(currentPosition);
            oldItem.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        currentPosition = position;
        musicPosition = currentPosition;
        downVideoMusic(item);
        if (progressDialog == null) progressDialog = new ProgressDialog(getActivity());
        progressDialog.show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MusicUtils.getMusicUtils().getMediaPlayr().destoryMediaPlayer();

    }

    @Override
    public void onPause() {
        super.onPause();
        MusicUtils.getMusicUtils().getMediaPlayr().pauseMediaPlayer();
    }

    /**
     * 下载音乐
     *
     * @param item
     */
    private void downVideoMusic(VideoMusicBean item) {
        if (item == null) return;
        savePath = VideoUtils.VIDEO_MUSIC + VideoHelper.getIntence().splitVideoName(item.getMusicUrl());
        FileDownUtils.getIntance().starSingleDown(getContext(), item.getMusicUrl(), savePath, new FileDownUtils.OnCompleteListener() {
            @Override
            public void onComplete() {
                progressDialog.disMissDialog();
                if (TextUtils.isEmpty(savePath)) return;
                MusicUtils.getMusicUtils().getMediaPlayr().setMediaPlayerUrl(savePath);
                MusicUtils.getMusicUtils().getMediaPlayr().playMediaPlayer();
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onPregress(int progress) {
                progressDialog.setmProgressbar(progress);
            }

            @Override
            public void onError() {
                progressDialog.disMissDialog();
                savePath = null;
                MusicUtils.getMusicUtils().getMediaPlayr().destoryMediaPlayer();
            }
        });
    }

    @Override
    public void onLoadMoreRequested() {
        pageNo++;
        mPresenter.getMusicVideoData(pageNo, Config.PAGE_SIZE);
    }


    @Override
    protected int getTopOffset() {
        return AppUtils.getScreenWidthAndHeight(getActivity())[1] / 100 * 8;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_video_music;
    }


    @OnClick({R.id.iv_close, R.id.bt_sure, R.id.delete_music})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                if (onDownVideoMusicListener != null) {
                    onDownVideoMusicListener.dissFragmentDialog();
                }
                break;
            case R.id.bt_sure:
                if (onDownVideoMusicListener != null) {
                        onDownVideoMusicListener.selectMusic(currentPosition, savePath);
                    onDownVideoMusicListener.dissFragmentDialog();
                }
                break;
            case R.id.delete_music:
                if (onDownVideoMusicListener != null) {
                    onDownVideoMusicListener.selectMusic(-1, null);
                    onDownVideoMusicListener.dissFragmentDialog();
                }
                break;
        }
    }

    public void setOnDownVideoMusicListener(OnDownVideoMusicListener onDownVideoMusicListener) {
        this.onDownVideoMusicListener = onDownVideoMusicListener;
    }

    public interface OnDownVideoMusicListener {
        void selectMusic(int position, String musicUrl);

        void dissFragmentDialog();
    }


    public void setMusicPosition(int musicPosition) {
        this.musicPosition = musicPosition;
    }
}
